module.exports = function (io) {
    var socketList = {}
    var member = 0
    var users = {}

    io.sockets.on('connection', (socket) => {
        console.log('socket链接成功!！')

        // 加入
        socket.on('join', (data) => {
            // console.log(data)
            socket.name = data.name
            socketList.name = data.userId
            member++
            let user = {name: data.name, avatar: data.avatar, userId: data.userId}
            // users.push(user)
            users = user
            // 获取自己
            socket.emit('getMyself', users)
        })

        // 一对一聊天
        socket.on('OneByOneMessage', (data) => {
            console.log('发送信息成功-->', data)

            // // 广播信息(给除了自己以外的客户端广播消息)
            socket.broadcast.emit('SendOneByOneMessage', data)

            // 一对一广播
            // socket.to(data.tid).emit('SendOneByOneMessage', data)
        })

        // 当客户端断开连接时
        socket.on('disconnect', () => {
            console.log('链接已断开！');
            if(socketList.hasOwnProperty(socket.name)){
                delete socketList[socket.name]
                for(var i =0 ;i<users.length;i++){
                    if(users[i].name == socket.name){
                        users.splice(i ,1)
                    }
                }
            }
        });
    })
}
